const bcrypt = require('bcrypt');

class User {
    constructor(data) {
        this.id = data.id;
        this.username = data.username;
        this.passwordHash = data.passwordHash;
        this.role = data.role;
        this.createdAt = data.createdAt;
        this.updatedAt = data.updatedAt;
    }

    // Verify password
    async verifyPassword(password) {
        return bcrypt.compare(password, this.passwordHash);
    }

    // Check if user is admin
    isAdmin() {
        return this.role === 'admin';
    }

    // Get user data without sensitive information
    toJSON() {
        return {
            id: this.id,
            username: this.username,
            role: this.role,
            createdAt: this.createdAt,
            updatedAt: this.updatedAt
        };
    }

    // Static method to create user from database data
    static fromDB(data) {
        return new User({
            id: data.id,
            username: data.username,
            passwordHash: data.passwordHash,
            role: data.role,
            createdAt: data.created_at,
            updatedAt: data.updated_at
        });
    }
}

module.exports = User;